package com.briup.xml;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.awt.image.ImageProducer;
import java.util.List;
import java.util.stream.Collectors;

/**
 * dom4j 解析xml
 */
public class Dom4jParse {
    public static void main(String[] args) {
        //创建解析器
        SAXReader reader = new SAXReader();
        try {
            //加载xml文件
            Document document = reader.read("C:\\Users\\sxx\\IdeaProjects\\bttc-java-basic-code\\02code\\src\\com\\briup\\xml\\file\\class.xml");
            //获取根节点
            Element root = document.getRootElement();
            //获取所有的子节点（stu）
            List<Element> elements = root.elements();
            elements.stream()
                    .map(i -> {
                        //获取节点的id属性
                        Attribute idAtt = i.attribute("id");
                        //获取属性的值
                        String id = idAtt.getValue();
                        //通过指定名字去获取子元素
                        Element nameNode = i.element("name");
                        String name = nameNode.getTextTrim();
                        int age = Integer.parseInt(i.element("age").getTextTrim());
                        return new Stu(id, name, age);
                    }).forEach(System.out::println);
//                    .collect(Collectors.toList());

        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }
}
